﻿using Dt261.help;
using Dt261.Model;
using Dt261.ViewModel;
using Panuon.UI.Silver;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Dt261.Views
{
    /// <summary>
    /// PreviewPage.xaml 的交互逻辑
    /// </summary>
    public partial class PreviewPage : Page
    {
        private string Bindtitle = "听力测试报告";

        private Collection<string> hzs = new Collection<string> { "125", "250", "500", "1K", "2K", "4K", "8K" };
        private Collection<int> hzsint = new Collection<int> { 125, 250, 500, 750, 1000, 1500, 2000, 3000, 4000, 6000, 8000 };

        Dictionary<int, double> hzsPxValue = new Dictionary<int, double> { { 125, 0 }, { 250, 1 }, { 500, 2 },{ 750, 2.5 }, { 1000, 3 }, { 1500, 3.5 }, { 2000, 4 },
            { 3000, 4.5 }, { 4000, 5 },{ 6000, 5.5 }, { 8000, 6 } };

        //Dictionary<int, int> limitHz = new Dictionary<int, int> { { 125, 65 }, { 250, 85 }, { 500, 105 }, { 750, 105 },{ 1000, 105 }, { 1500, 105 }, { 2000, 105 },
        //    { 3000, 105 }, { 4000, 105 },{ 6000, 105 }, { 8000, 95 } };
        private int topM =36;
        private int rightM = 20;
        private double px;
        private double py;

        private List<Point> dataPoints = new List<Point>();
        private PointCollection coordinatePoints = new PointCollection();
        private List<Shape> pointEllipses = new List<Shape>();
        private List<Shape> pointEllipses2 = new List<Shape>();


        private List<Shape> pointBcEllipses = new List<Shape>();
        private List<Shape> pointBcEllipses2 = new List<Shape>();//
        Polyline curvePolyline = new Polyline();

        private Point yuandian = new Point(20, 280);

        public PreviewPage(PureToneResult result, MemberInfo member)
        {
            InitializeComponent();




            curvePolyline.Stroke = Brushes.Blue;
            curvePolyline.StrokeThickness = 2;

            px = (this.chartCanvas.Width - yuandian.X - rightM) / 6.0;
            py = (yuandian.Y - topM) / 13.0;

            DrawScale(this.chartCanvas, Color.FromRgb(0xd9, 0x4f, 0x54));
       
            //DrawPoint(this.chartCanvas, pointEllipses);
            //DrawCurve(this.chartCanvas);

            DrawScale(this.chartCanvas2, Color.FromRgb(0x55, 0xa4, 0xdd));
           

            PreviewPageViewModel model = new PreviewPageViewModel();
            this.DataContext = model;
            model.SelectPureToneResultEvent += UpdateTestItem;
            model.SelecTabEvent += selectTabChanged;
            model.SelectMember = member;
            model.SelectPureToneResult = result;
            //model.getInitInfo();
        }

        private void TitleButton_Click(object sender, RoutedEventArgs e)
        {
            PreviewPageViewModel model = (PreviewPageViewModel)this.DataContext;
            model.SaveTitleInfo();
        }


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            UserPageViewModel model = (UserPageViewModel)this.DataContext;
            // Broadcast.PushBroadcast("Navigation", new Page1());
            if (model.SelectMember == null)
            {
                //var result = MessageBoxX.Show("请先添加测试成员", "提醒", Application.Current.MainWindow, MessageBoxButton.OK, new MessageBoxXConfigurations()
                //{
                //    MessageBoxStyle = MessageBoxStyle.Modern,
                //    MessageBoxIcon = MessageBoxIcon.Warning,
                //    ButtonBrush = "#F1C825".ToColor().ToBrush(),
                //});

                //if (result == MessageBoxResult.Yes)
                //{

                //    DMSkin.Core.Broadcast.PushBroadcast("Navigation", new UserInfoPage(model.SelectMember));
                //    // DialogService.ShowMessage("Delete friend successfully");
                //}
            }
            else
            {
                new TestWindow(model.SelectMember).ShowDialog();
            }



        }

        void selectTabChanged(int selectIndex = 0)
        {
            var m = GetPIC(chartCanvas);
            chartCanvas3Image.Source = GetPIC(chartCanvas);

            chartCanvas4Image.Source = GetPIC(chartCanvas2);
        }

        private void Button_Click2(object sender, RoutedEventArgs e)
        {
            new TestWindow().ShowDialog();
        }
        private void UpdateTestItem(Dictionary<int, TestItem> list, bool isRight, int mmtype = 0, int bcneedlink = 0)
        {

            ThreadPool.QueueUserWorkItem(delegate
            {
                SynchronizationContext.SetSynchronizationContext(new
                    DispatcherSynchronizationContext(System.Windows.Application.Current.Dispatcher));
                SynchronizationContext.Current.Post(pl =>
                {
                    if (mmtype == 0)
                    {
                        if (isRight)
                        {
                            foreach (Shape dataEllipse in pointEllipses)
                            {
                                this.chartCanvas.Children.Remove(dataEllipse);
                            }

                            if (list == null) return;

                            TestItem lastitem = null;
                            for (int i = 0; i < this.hzsint.Count(); i++)
                            {
                                //(40,280)原点坐标,上面空45，右边空80
                                // this.chartCanvas.Children.Clear();
                                if (list.ContainsKey(this.hzsint[i]))
                                {
                                    TestItem item = list[this.hzsint[i]];
                                    if (item.Mask == true)
                                    {
                                        TLBIcon.DrawIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
                                            topM + (10 + item.Db) / 10.0 * py,
                                            item.MarkType != 0 ? item.MarkType : 10,
                                            true,
                                            this.chartCanvas,
                                            pointEllipses);

                                    }
                                    else
                                    {


                                        TLBIcon.DrawIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
                                 topM + (10 + item.Db) / 10.0 * py,
                                 item.MarkType != 0 ? item.MarkType : 1,
                                 true,
                                 this.chartCanvas,
                                 pointEllipses);
                                    }



                                    if (item.IsUserResp != true)
                                    {
                                        TLBIcon.DrawNoRespIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
                                           topM + (10 + item.Db) / 10.0 * py,
                                           isRight,
                                            this.chartCanvas,
                                           pointEllipses);

                                    }


                                    if (lastitem != null)
                                    {
                                        Line line = new Line();
                                        line.Stroke = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));

                                        line.Y1 = topM + (10 + lastitem.Db) / 10.0 * py;
                                        line.X1 = yuandian.X + hzsPxValue[lastitem.Hz] * px;
                                        line.Y2 = topM + (10 + item.Db) / 10.0 * py;
                                        line.X2 = yuandian.X + hzsPxValue[item.Hz] * px;
                                        line.StrokeThickness = 1;
                                        this.chartCanvas.Children.Add(line);
                                        pointEllipses.Add(line);

                                    }


                                    lastitem = item;



                                }

                            }

                        }
                        else
                        {
                            foreach (Shape dataEllipse in pointEllipses2)
                            {
                                this.chartCanvas2.Children.Remove(dataEllipse);
                            }

                            if (list == null) return;
                            TestItem lastitem = null;
                            for (int i = 0; i < this.hzsint.Count(); i++)
                            {
                                //(40,280)原点坐标,上面空45，右边空80
                                // this.chartCanvas.Children.Clear();
                                if (list.ContainsKey(this.hzsint[i]))
                                {
                                    TestItem item = list[this.hzsint[i]];

                                    if (item.IsUserResp != true)
                                    {
                                        TLBIcon.DrawNoRespIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
                                           topM + (10 + item.Db) / 10.0 * py,
                                           isRight,
                                            this.chartCanvas2,
                                           pointEllipses2);

                                    }


                                    if (item.Mask == true)
                                    {


                                        TLBIcon.DrawIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
                                 topM + (10 + item.Db) / 10.0 * py,
                                 item.MarkType != 0 ? item.MarkType : 9,
                                 false,
                                 this.chartCanvas2,
                                 pointEllipses2);
                                    }
                                    else
                                    {
                                        TLBIcon.DrawIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
                topM + (10 + item.Db) / 10.0 * py,
                item.MarkType != 0 ? item.MarkType : 2,
                false,
                this.chartCanvas2,
                pointEllipses2);

                                    }


                                    if (lastitem != null)
                                    {
                                        Line line = new Line();
                                        line.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));

                                        line.Y1 = topM + (10 + lastitem.Db) / 10.0 * py;
                                        line.X1 = yuandian.X + hzsPxValue[lastitem.Hz] * px;
                                        line.Y2 = topM + (10 + item.Db) / 10.0 * py;
                                        line.X2 = yuandian.X + hzsPxValue[(item.Hz)] * px;
                                        line.StrokeThickness = 1;


                                        this.chartCanvas2.Children.Add(line);
                                        pointEllipses2.Add(line);

                                    }

                                    lastitem = item;



                                }

                            }
                        }
                    }


                    else if (mmtype == 1)
                    {
                        if (isRight)
                        {
                            foreach (Shape dataEllipse in pointBcEllipses)
                            {
                                this.chartCanvas.Children.Remove(dataEllipse);
                            }

                            if (list == null) return;
                            TestWindowViewModel vm = DataContext as TestWindowViewModel;


                            TestItem lastitem = null;
                            for (int i = 0; i < this.hzsint.Count(); i++)
                            {
                                //(40,280)原点坐标,上面空45，右边空80
                                // this.chartCanvas.Children.Clear();
                                if (list.ContainsKey(this.hzsint[i]))
                                {
                                    TestItem item = list[this.hzsint[i]];


                                    if (item.Mask == true)
                                    {
                                        TLBIcon.DrawIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
                                      topM + (10 + item.Db) / 10.0 * py,
                                      item.MarkType != 0 ? item.MarkType : 6,
                                      isRight,
                                      this.chartCanvas,
                                      pointBcEllipses);



                                    }
                                    else
                                    {
                                        TLBIcon.DrawIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
                                           topM + (10 + item.Db) / 10.0 * py,
                                           item.MarkType != 0 ? item.MarkType : 4,
                                           isRight,
                                           this.chartCanvas,
                                           pointBcEllipses);

                                    }





                                    if (item.IsUserResp != true)
                                    {

                                        TLBIcon.DrawNoRespIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
                                   topM + (10 + item.Db) / 10.0 * py,
                                   isRight,
                                    this.chartCanvas,
                                   pointBcEllipses);

                                    }

                                    //骨导默认不连线
                                    if (lastitem != null && bcneedlink == 1)
                                    {
                                   
                                        Polyline x_scale = new Polyline();
                                        x_scale.Stroke = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));
                                        x_scale.StrokeDashArray.Add(10);
                                        x_scale.StrokeDashArray.Add(20);
                                        ;
                                        x_scale.StrokeThickness = 0.5;
                                        x_scale.Points.Add(new Point(yuandian.X + hzsPxValue[lastitem.Hz] * px, topM + (10 + lastitem.Db) / 10.0 * py));
                                        x_scale.Points.Add(new Point(yuandian.X + hzsPxValue[item.Hz] * px, topM + (10 + item.Db) / 10.0 * py));


                                        this.chartCanvas.Children.Add(x_scale);
                                        pointBcEllipses.Add(x_scale);

                                    }


                                    lastitem = item;



                                }

                            }

                        }
                        else
                        {
                            foreach (Shape dataEllipse in pointBcEllipses2)
                            {
                                this.chartCanvas2.Children.Remove(dataEllipse);
                            }

                            if (list == null) return;
                            PreviewPageViewModel vm = DataContext as PreviewPageViewModel;


                            TestItem lastitem = null;
                            for (int i = 0; i < this.hzsint.Count(); i++)
                            {
                                //(40,280)原点坐标,上面空45，右边空80
                                // this.chartCanvas.Children.Clear();
                                if (list.ContainsKey(this.hzsint[i]))
                                {
                                    TestItem item = list[this.hzsint[i]];

                                    if (item.IsUserResp != true)
                                    {
                                        TLBIcon.DrawNoRespIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
                                     topM + (10 + item.Db) / 10.0 * py,
                                     isRight,
                                      this.chartCanvas2,
                                     pointBcEllipses2);

                                    }


                                    if (item.Mask == true)
                                    {
                                        TLBIcon.DrawIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
                                      topM + (10 + item.Db) / 10.0 * py,
                                      item.MarkType != 0 ? item.MarkType : 5,
                                      isRight,
                                      this.chartCanvas2,
                                      pointBcEllipses2);



                                    }
                                    else
                                    {
                                        TLBIcon.DrawIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
                                           topM + (10 + item.Db) / 10.0 * py,
                                           item.MarkType != 0 ? item.MarkType : 3,
                                           isRight,
                                           this.chartCanvas2,
                                           pointBcEllipses2);

                                    }

                                    if (lastitem != null && bcneedlink == 1)
                                    {
                    

                                        Polyline x_scale = new Polyline();
                                        x_scale.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));
                                        x_scale.StrokeDashArray.Add(10);
                                        x_scale.StrokeDashArray.Add(20);
                                        ;
                                        x_scale.StrokeThickness = 0.5;
                                        x_scale.Points.Add(new Point(yuandian.X + hzsPxValue[lastitem.Hz] * px, topM + (10 + lastitem.Db) / 10.0 * py));
                                        x_scale.Points.Add(new Point(yuandian.X + hzsPxValue[item.Hz] * px, topM + (10 + item.Db) / 10.0 * py));


                                        this.chartCanvas2.Children.Add(x_scale);
                                        pointBcEllipses2.Add(x_scale);



                                    }

                                    lastitem = item;



                                }

                            }
                        }
                    }

                }, null);
            });
        }
        private void UpdateTestItem2(Dictionary<int, TestItem> list, bool isRight, int mmtype = 0)
        {
            ThreadPool.QueueUserWorkItem(delegate
            {
                SynchronizationContext.SetSynchronizationContext(new
                    DispatcherSynchronizationContext(System.Windows.Application.Current.Dispatcher));
                SynchronizationContext.Current.Post(pl =>
                {
                    if (mmtype == 0)
                    {
                        if (isRight)
                        {
                            foreach (Shape dataEllipse in pointEllipses)
                            {
                                this.chartCanvas.Children.Remove(dataEllipse);
                            }

                            if (list == null) return;

                            TestItem lastitem = null;
                            for (int i = 0; i < this.hzsint.Count(); i++)
                            {
                                //(40,280)原点坐标,上面空45，右边空80
                                // this.chartCanvas.Children.Clear();
                                if (list.ContainsKey(this.hzsint[i]))
                                {
                                    TestItem item = list[this.hzsint[i]];
                                    if (item.Mask == true)
                                    {
                                        {
                                            Line line = new Line();
                                            line.Stroke = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));

                                            line.Y1 = topM + (10 + item.Db) / 10.0 * py - 5;
                                            line.X1 = yuandian.X + hzsPxValue[item.Hz] * px;
                                            line.Y2 = line.Y1 + 10;
                                            line.X2 = line.X1 - 5;
                                            line.StrokeThickness = 1;
                                            this.chartCanvas.Children.Add(line);
                                            pointEllipses.Add(line);
                                        }
                                        {
                                            Line line = new Line();
                                            line.Stroke = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));

                                            line.Y1 = topM + (10 + item.Db) / 10.0 * py - 5;
                                            line.X1 = yuandian.X + hzsPxValue[item.Hz] * px;
                                            line.Y2 = line.Y1 + 10;
                                            line.X2 = line.X1 + 5;
                                            line.StrokeThickness = 1;
                                            this.chartCanvas.Children.Add(line);
                                            pointEllipses.Add(line);
                                        }
                                        {
                                            Line line = new Line();
                                            line.Stroke = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));

                                            line.Y1 = topM + (10 + item.Db) / 10.0 * py + 5;
                                            line.X1 = yuandian.X + hzsPxValue[item.Hz] * px - 5;
                                            line.Y2 = line.Y1;
                                            line.X2 = line.X1 + 10;
                                            line.StrokeThickness = 1;
                                            this.chartCanvas.Children.Add(line);
                                            pointEllipses.Add(line);
                                        }
                                    }
                                    else
                                    {
                                        Ellipse dataEllipse = new Ellipse();
                                        // dataEllipse.Fill = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));
                                        dataEllipse.Stroke = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));
                                        dataEllipse.Width = 12;
                                        dataEllipse.Height = 12;
                                        Canvas.SetTop(dataEllipse, topM + (10 + item.Db) / 10.0 * py - 6);
                                        Canvas.SetLeft(dataEllipse, yuandian.X + hzsPxValue[item.Hz] * px - 6);
                                        this.chartCanvas.Children.Add(dataEllipse);
                                        pointEllipses.Add(dataEllipse);
                                    }



                                    if (item.IsUserResp != true)
                                    {
                                        {
                                            Line line = new Line();
                                            line.Stroke = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));

                                            line.Y1 = topM + (10 + item.Db) / 10.0 * py + 4;
                                            line.X1 = yuandian.X + hzsPxValue[item.Hz] * px - 4;
                                            line.Y2 = line.Y1 + 4;
                                            line.X2 = line.X1 - 4;
                                            line.StrokeThickness = 1;
                                            this.chartCanvas.Children.Add(line);
                                            pointEllipses.Add(line);
                                        }
                                        {
                                            Line line = new Line();
                                            line.Stroke = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));

                                            line.Y1 = topM + (10 + item.Db) / 10.0 * py + 8;
                                            line.X1 = yuandian.X + hzsPxValue[item.Hz] * px - 8;
                                            line.Y2 = line.Y1 - 4;
                                            line.X2 = line.X1;
                                            line.StrokeThickness = 1;
                                            this.chartCanvas.Children.Add(line);
                                            pointEllipses.Add(line);
                                        }
                                        {
                                            Line line = new Line();
                                            line.Stroke = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));

                                            line.Y1 = topM + (10 + item.Db) / 10.0 * py + 8;
                                            line.X1 = yuandian.X + hzsPxValue[item.Hz] * px - 8;
                                            line.Y2 = line.Y1;
                                            line.X2 = line.X1 + 4;
                                            line.StrokeThickness = 1;
                                            this.chartCanvas.Children.Add(line);
                                            pointEllipses.Add(line);
                                        }
                                    }


                                    if (lastitem != null)
                                    {
                                        Line line = new Line();
                                        line.Stroke = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));

                                        line.Y1 = topM + (10 + lastitem.Db) / 10.0 * py;
                                        line.X1 = yuandian.X + hzsPxValue[lastitem.Hz] * px;
                                        line.Y2 = topM + (10 + item.Db) / 10.0 * py;
                                        line.X2 = yuandian.X + hzsPxValue[item.Hz] * px;
                                        line.StrokeThickness = 1;
                                        this.chartCanvas.Children.Add(line);
                                        pointEllipses.Add(line);

                                    }


                                    lastitem = item;



                                }

                            }

                        }
                        else
                        {
                            foreach (Shape dataEllipse in pointEllipses2)
                            {
                                this.chartCanvas2.Children.Remove(dataEllipse);
                            }

                            if (list == null) return;
                            TestItem lastitem = null;
                            for (int i = 0; i < this.hzsint.Count(); i++)
                            {
                                //(40,280)原点坐标,上面空45，右边空80
                                // this.chartCanvas.Children.Clear();
                                if (list.ContainsKey(this.hzsint[i]))
                                {
                                    TestItem item = list[this.hzsint[i]];

                                    if (item.IsUserResp != true)
                                    {
                                        {
                                            Line line = new Line();
                                            line.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));

                                            line.Y1 = topM + (10 + item.Db) / 10.0 * py + 4;
                                            line.X1 = yuandian.X + hzsPxValue[item.Hz] * px - 4;
                                            line.Y2 = line.Y1 + 4;
                                            line.X2 = line.X1 - 4;
                                            line.StrokeThickness = 1;
                                            this.chartCanvas2.Children.Add(line);
                                            pointEllipses2.Add(line);
                                        }
                                        {
                                            Line line = new Line();
                                            line.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));

                                            line.Y1 = topM + (10 + item.Db) / 10.0 * py + 8;
                                            line.X1 = yuandian.X + hzsPxValue[item.Hz] * px - 8;
                                            line.Y2 = line.Y1 - 4;
                                            line.X2 = line.X1;
                                            line.StrokeThickness = 1;
                                            this.chartCanvas2.Children.Add(line);
                                            pointEllipses2.Add(line);
                                        }
                                        {
                                            Line line = new Line();
                                            line.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));

                                            line.Y1 = topM + (10 + item.Db) / 10.0 * py + 8;
                                            line.X1 = yuandian.X + hzsPxValue[item.Hz] * px - 8;
                                            line.Y2 = line.Y1;
                                            line.X2 = line.X1 + 4;
                                            line.StrokeThickness = 1;
                                            this.chartCanvas2.Children.Add(line);
                                            pointEllipses2.Add(line);
                                        }
                                    }


                                    if (item.Mask == true)
                                    {
                                        Rectangle rect = new Rectangle();
                                        //(40 + px * hzsint.IndexOf(hz), 280, px, py * (120 - value));

                                        rect.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));

                                        rect.Height = 10.0;


                                        rect.Width = 10.0;
                                        Canvas.SetLeft(rect, yuandian.X + hzsPxValue[item.Hz] * px - 5);
                                        Canvas.SetTop(rect, topM + (10 + item.Db) / 10.0 * py - 5);

                                        this.chartCanvas2.Children.Add(rect);
                                        pointEllipses2.Add(rect);
                                    }
                                    else
                                    {
                                        {
                                            Line line = new Line();
                                            line.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));
                                            line.Y1 = topM + (10 + item.Db) / 10.0 * py - 6;
                                            line.X1 = yuandian.X + hzsPxValue[item.Hz] * px - 6;
                                            line.Y2 = topM + (10 + item.Db) / 10.0 * py + 6;
                                            line.X2 = yuandian.X + hzsPxValue[item.Hz] * px + 6;
                                            line.StrokeThickness = 1;
                                            this.chartCanvas2.Children.Add(line);
                                            pointEllipses2.Add(line);
                                        }
                                        {
                                            Line line = new Line();
                                            line.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));
                                            line.Y1 = topM + (10 + item.Db) / 10.0 * py + 6;
                                            line.X1 = yuandian.X + hzsPxValue[item.Hz] * px - 6;
                                            line.Y2 = topM + (10 + item.Db) / 10.0 * py - 6;
                                            line.X2 = yuandian.X + hzsPxValue[item.Hz] * px + 6;
                                            line.StrokeThickness = 1;
                                            this.chartCanvas2.Children.Add(line);
                                            pointEllipses2.Add(line);
                                        }
                                    }


                                    if (lastitem != null)
                                    {
                                        Line line = new Line();
                                        line.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));

                                        line.Y1 = topM + (10 + lastitem.Db) / 10.0 * py;
                                        line.X1 = yuandian.X + hzsPxValue[lastitem.Hz] * px;
                                        line.Y2 = topM + (10 + item.Db) / 10.0 * py;
                                        line.X2 = yuandian.X + hzsPxValue[(item.Hz)] * px;
                                        line.StrokeThickness = 1;


                                        this.chartCanvas2.Children.Add(line);
                                        pointEllipses2.Add(line);

                                    }

                                    lastitem = item;



                                }

                            }
                        }
                    }


                    else if (mmtype == 1)
                    {
                        if (isRight)
                        {
                            foreach (Shape dataEllipse in pointBcEllipses)
                            {
                                this.chartCanvas.Children.Remove(dataEllipse);
                            }


                            TestWindowViewModel vm = DataContext as TestWindowViewModel;


                            TestItem lastitem = null;
                            for (int i = 0; i < this.hzsint.Count(); i++)
                            {
                                //(40,280)原点坐标,上面空45，右边空80
                                // this.chartCanvas.Children.Clear();
                                if (list.ContainsKey(this.hzsint[i]))
                                {
                                    TestItem item = list[this.hzsint[i]];


                                    if (item.Mask == true)
                                    {
                                        {
                                            Line line = new Line();
                                            line.Stroke = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));

                                            line.Y1 = topM + (10 + item.Db) / 10.0 * py - 5;
                                            line.X1 = yuandian.X + hzsPxValue[item.Hz] * px - 5;
                                            line.Y2 = line.Y1 + 10;
                                            line.X2 = line.X1;
                                            line.StrokeThickness = 1;
                                            this.chartCanvas.Children.Add(line);
                                            pointBcEllipses.Add(line);
                                        }
                                        {
                                            Line line = new Line();
                                            line.Stroke = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));

                                            line.Y1 = topM + (10 + item.Db) / 10.0 * py - 5;
                                            line.X1 = yuandian.X + hzsPxValue[item.Hz] * px - 5;
                                            line.Y2 = line.Y1 + 0;
                                            line.X2 = line.X1 + 10;
                                            line.StrokeThickness = 1;
                                            this.chartCanvas.Children.Add(line);
                                            pointBcEllipses.Add(line);
                                        }
                                        {
                                            Line line = new Line();
                                            line.Stroke = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));

                                            line.Y1 = topM + (10 + item.Db) / 10.0 * py + 5;
                                            line.X1 = yuandian.X + hzsPxValue[item.Hz] * px - 5;
                                            line.Y2 = line.Y1;
                                            line.X2 = line.X1 + 10;
                                            line.StrokeThickness = 1;
                                            this.chartCanvas.Children.Add(line);
                                            pointBcEllipses.Add(line);
                                        }


                                    }
                                    else
                                    {
                                        {
                                            Line line = new Line();
                                            line.Stroke = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));

                                            line.Y1 = topM + (10 + item.Db) / 10.0 * py;
                                            line.X1 = yuandian.X + hzsPxValue[item.Hz] * px - 5;
                                            line.Y2 = line.Y1 + 5;
                                            line.X2 = line.X1 + 10;
                                            line.StrokeThickness = 1;
                                            this.chartCanvas.Children.Add(line);
                                            pointBcEllipses.Add(line);
                                        }
                                        {
                                            Line line = new Line();
                                            line.Stroke = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));

                                            line.Y1 = topM + (10 + item.Db) / 10.0 * py;
                                            line.X1 = yuandian.X + hzsPxValue[item.Hz] * px - 5;
                                            line.Y2 = line.Y1 - 5;
                                            line.X2 = line.X1 + 10;
                                            line.StrokeThickness = 1;
                                            this.chartCanvas.Children.Add(line);
                                            pointBcEllipses.Add(line);
                                        }

                                    }





                                    if (item.IsUserResp != true)
                                    {
                                        {
                                            Line line = new Line();
                                            line.Stroke = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));

                                            line.Y1 = topM + (10 + item.Db) / 10.0 * py + 4;
                                            line.X1 = yuandian.X + hzsPxValue[item.Hz] * px - 4;
                                            line.Y2 = line.Y1 + 4;
                                            line.X2 = line.X1 - 4;
                                            line.StrokeThickness = 1;
                                            this.chartCanvas.Children.Add(line);
                                            pointEllipses.Add(line);
                                        }
                                        {
                                            Line line = new Line();
                                            line.Stroke = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));

                                            line.Y1 = topM + (10 + item.Db) / 10.0 * py + 8;
                                            line.X1 = yuandian.X + hzsPxValue[item.Hz] * px - 8;
                                            line.Y2 = line.Y1 - 4;
                                            line.X2 = line.X1;
                                            line.StrokeThickness = 1;
                                            this.chartCanvas.Children.Add(line);
                                            pointEllipses.Add(line);
                                        }
                                        {
                                            Line line = new Line();
                                            line.Stroke = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));

                                            line.Y1 = topM + (10 + item.Db) / 10.0 * py + 8;
                                            line.X1 = yuandian.X + hzsPxValue[item.Hz] * px - 8;
                                            line.Y2 = line.Y1;
                                            line.X2 = line.X1 + 4;
                                            line.StrokeThickness = 1;
                                            this.chartCanvas.Children.Add(line);
                                            pointEllipses.Add(line);
                                        }
                                    }

                                    //骨导不连线
                                    //if (lastitem != null)
                                    //{
                                    //    Line line = new Line();
                                    //    line.Stroke = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));

                                    //    line.Y1 = topM + (10 + lastitem.Db) / 10.0 * py;
                                    //    line.X1 = yuandian.X + hzsPxValue[lastitem.Hz] * px;
                                    //    line.Y2 = topM + (10 + item.Db) / 10.0 * py;
                                    //    line.X2 = yuandian.X + hzsPxValue[item.Hz] * px;
                                    //    line.StrokeThickness = 1;
                                    //    this.chartCanvas.Children.Add(line);
                                    //    pointEllipses.Add(line);

                                    //}


                                    lastitem = item;



                                }

                            }

                        }
                        else
                        {
                            foreach (Shape dataEllipse in pointBcEllipses2)
                            {
                                this.chartCanvas2.Children.Remove(dataEllipse);
                            }


                            TestWindowViewModel vm = DataContext as TestWindowViewModel;


                            TestItem lastitem = null;
                            for (int i = 0; i < this.hzsint.Count(); i++)
                            {
                                //(40,280)原点坐标,上面空45，右边空80
                                // this.chartCanvas.Children.Clear();
                                if (list.ContainsKey(this.hzsint[i]))
                                {
                                    TestItem item = list[this.hzsint[i]];

                                    if (item.IsUserResp != true)
                                    {
                                        {
                                            Line line = new Line();
                                            line.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));

                                            line.Y1 = topM + (10 + item.Db) / 10.0 * py + 4;
                                            line.X1 = yuandian.X + hzsPxValue[item.Hz] * px - 4;
                                            line.Y2 = line.Y1 + 4;
                                            line.X2 = line.X1 - 4;
                                            line.StrokeThickness = 1;
                                            this.chartCanvas2.Children.Add(line);
                                            pointEllipses2.Add(line);
                                        }
                                        {
                                            Line line = new Line();
                                            line.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));

                                            line.Y1 = topM + (10 + item.Db) / 10.0 * py + 8;
                                            line.X1 = yuandian.X + hzsPxValue[item.Hz] * px - 8;
                                            line.Y2 = line.Y1 - 4;
                                            line.X2 = line.X1;
                                            line.StrokeThickness = 1;
                                            this.chartCanvas2.Children.Add(line);
                                            pointEllipses2.Add(line);
                                        }
                                        {
                                            Line line = new Line();
                                            line.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));

                                            line.Y1 = topM + (10 + item.Db) / 10.0 * py + 8;
                                            line.X1 = yuandian.X + hzsPxValue[item.Hz] * px - 8;
                                            line.Y2 = line.Y1;
                                            line.X2 = line.X1 + 4;
                                            line.StrokeThickness = 1;
                                            this.chartCanvas2.Children.Add(line);
                                            pointEllipses2.Add(line);
                                        }
                                    }


                                    if (item.Mask == true)
                                    {
                                        {
                                            Line line = new Line();
                                            line.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));

                                            line.Y1 = topM + (10 + item.Db) / 10.0 * py - 5;
                                            line.X1 = yuandian.X + hzsPxValue[item.Hz] * px + 5;
                                            line.Y2 = line.Y1 + 10;
                                            line.X2 = line.X1;
                                            line.StrokeThickness = 1;
                                            this.chartCanvas2.Children.Add(line);
                                            pointBcEllipses2.Add(line);
                                        }
                                        {
                                            Line line = new Line();
                                            line.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));

                                            line.Y1 = topM + (10 + item.Db) / 10.0 * py - 5;
                                            line.X1 = yuandian.X + hzsPxValue[item.Hz] * px - 5;
                                            line.Y2 = line.Y1 + 0;
                                            line.X2 = line.X1 + 10;
                                            line.StrokeThickness = 1;
                                            this.chartCanvas2.Children.Add(line);
                                            pointBcEllipses2.Add(line);
                                        }
                                        {
                                            Line line = new Line();
                                            line.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));

                                            line.Y1 = topM + (10 + item.Db) / 10.0 * py + 5;
                                            line.X1 = yuandian.X + hzsPxValue[item.Hz] * px - 5;
                                            line.Y2 = line.Y1;
                                            line.X2 = line.X1 + 10;
                                            line.StrokeThickness = 1;
                                            this.chartCanvas2.Children.Add(line);
                                            pointBcEllipses2.Add(line);
                                        }
                                    }
                                    else
                                    {

                                        {
                                            Line line = new Line();
                                            line.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));
                                            line.Y1 = topM + (10 + item.Db) / 10.0 * py;
                                            line.X1 = yuandian.X + hzsPxValue[item.Hz] * px + 5;
                                            line.Y2 = line.Y1 + 5;
                                            line.X2 = line.X1 - 10;
                                            line.StrokeThickness = 1;
                                            this.chartCanvas2.Children.Add(line);
                                            pointBcEllipses2.Add(line);
                                        }
                                        {
                                            Line line = new Line();
                                            line.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));
                                            line.Y1 = topM + (10 + item.Db) / 10.0 * py;
                                            line.X1 = yuandian.X + hzsPxValue[item.Hz] * px + 5;
                                            line.Y2 = line.Y1 - 5;
                                            line.X2 = line.X1 - 10;
                                            line.StrokeThickness = 1;
                                            this.chartCanvas2.Children.Add(line);
                                            pointBcEllipses2.Add(line);
                                        }
                                    }



                                    //if (lastitem != null)
                                    //{
                                    //    Line line = new Line();
                                    //    line.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));

                                    //    line.Y1 = topM + (10 + lastitem.Db) / 10.0 * py;
                                    //    line.X1 = yuandian.X + hzsPxValue[lastitem.Hz] * px;
                                    //    line.Y2 = topM + (10 + item.Db) / 10.0 * py;
                                    //    line.X2 = yuandian.X + hzsPxValue[item.Hz] * px;
                                    //    line.StrokeThickness = 1;


                                    //    this.chartCanvas2.Children.Add(line);
                                    //    pointEllipses2.Add(line);

                                    //}

                                    lastitem = item;



                                }

                            }
                        }
                    }

                }, null);
            });
        }
        /// <summary>
        /// 作出箭头
        /// </summary>
        private void DrawArrow()
        {
            //System.Windows.Shapes.Path x_axisArrow = new Path();//x轴箭头
            //Path y_axisArrow = new Path();//y轴箭头

            //x_axisArrow.Fill = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));
            //y_axisArrow.Fill = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));

            //PathFigure x_axisFigure = new PathFigure();
            //x_axisFigure.IsClosed = true;
            //x_axisFigure.StartPoint = new Point(480, 276);                          //路径的起点
            //x_axisFigure.Segments.Add(new LineSegment(new Point(480, 284), false)); //第2个点
            //x_axisFigure.Segments.Add(new LineSegment(new Point(490, 280), false)); //第3个点

            //PathFigure y_axisFigure = new PathFigure();
            //y_axisFigure.IsClosed = true;
            //y_axisFigure.StartPoint = new Point(36, 30);                          //路径的起点
            //y_axisFigure.Segments.Add(new LineSegment(new Point(44, 30), false)); //第2个点
            //y_axisFigure.Segments.Add(new LineSegment(new Point(40, 20), false)); //第3个点

            //PathGeometry x_axisGeometry = new PathGeometry();
            //PathGeometry y_axisGeometry = new PathGeometry();

            //x_axisGeometry.Figures.Add(x_axisFigure);
            //y_axisGeometry.Figures.Add(y_axisFigure);

            //x_axisArrow.Data = x_axisGeometry;
            //y_axisArrow.Data = y_axisGeometry;

            //this.chartCanvas.Children.Add(x_axisArrow);
            //this.chartCanvas.Children.Add(y_axisArrow);
        }
        /// <summary>
        /// 作出x轴和y轴的标尺
        /// </summary>
        private void DrawScale(Canvas canvas, Color color)
        {

            //foreach (int hz in hzsint)
            //{
            //    int value = limitHz[hz];

            //    Rectangle rect = new Rectangle();
            //    //(40 + px * hzsint.IndexOf(hz), 280, px, py * (120 - value));
            //    rect.Fill = new SolidColorBrush(color);

            //    double index = hzsPxValue[hz];


            //    rect.Height = py * (120 - value) / 10.0;

            //    if (index == 0)
            //    {
            //        rect.Width = px / 2;
            //        Canvas.SetLeft(rect, yuandian.X);
            //    }
            //    else if (index == 6)
            //    {
            //        rect.Width = px / 2;
            //        Canvas.SetLeft(rect, yuandian.X + px * index - px / 2);
            //    }
            //    else
            //    {
            //        rect.Width = px;
            //        Canvas.SetLeft(rect, yuandian.X + px * index - px / 2);
            //    }

            //    Canvas.SetTop(rect, yuandian.Y - rect.Height);
            //    canvas.Children.Add(rect);

            //}


            for (int i = 0; i < 4; i += 1)//作480个刻度，因为当前x轴长 480px，每10px作一个小刻度，还预留了一些小空间
            {
                //原点 O=(40,280),s

                Polyline x_scale = new Polyline();
                x_scale.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0));
                x_scale.StrokeDashArray.Add(10);
                x_scale.StrokeDashArray.Add(20);
                /// Line x_scale = new Line();
                //x_scale.StrokeEndLineCap = PenLineCap.Triangle;
                //x_scale.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0));
                x_scale.StrokeThickness = 0.1;
                x_scale.Points.Add(new Point(yuandian.X + (2.5 + i) * (canvas.Width - yuandian.X - rightM) / 6.0, yuandian.Y));
                x_scale.Points.Add(new Point(yuandian.X + (2.5 + i) * (canvas.Width - yuandian.X - rightM) / 6.0, topM));    //在x轴上的刻度线，起点和终点相同

                //x_scale.Y1 = yuandian.Y;           //与原点坐标的y=280，相同 
                //x_scale.Y2 = topM;


                canvas.Children.Add(x_scale);

            }
            //(40,280)原点坐标,上面空45，右边空80

            //"1500", "3000", "6000",

            for (int i = 0; i < 7; i += 1)//作480个刻度，因为当前x轴长 480px，每10px作一个小刻度，还预留了一些小空间
            {
                //原点 O=(40,280),s
                Line x_scale = new Line();
                x_scale.StrokeEndLineCap = PenLineCap.Triangle;
                x_scale.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0));
                x_scale.StrokeThickness = 0.1;
                x_scale.X1 = yuandian.X + i * (canvas.Width - yuandian.X - rightM) / 6.0;
                x_scale.X2 = x_scale.X1;    //在x轴上的刻度线，起点和终点相同

                x_scale.Y1 = yuandian.Y;           //与原点坐标的y=280，相同 
                x_scale.Y2 = topM;


                canvas.Children.Add(x_scale);


                {
                    TextBlock x_ScaleLabel = new TextBlock();

                    x_ScaleLabel.TextAlignment = TextAlignment.Left;
                    x_ScaleLabel.Width = (canvas.Width - yuandian.X - rightM) / 6.0;
                    x_ScaleLabel.FontSize = 12;
                    x_ScaleLabel.Text = hzs[i]; ;//只给大刻度添加标签，每50px添加一个标签

                    Canvas.SetLeft(x_ScaleLabel, yuandian.X + i * (canvas.Width - yuandian.X - rightM) / 6.0 - 12);//40是原点的坐标，-12是为了让标签看的位置剧中一点
                    Canvas.SetTop(x_ScaleLabel, yuandian.Y + 10);//让标签字往下移一点

                    canvas.Children.Add(x_ScaleLabel);

                }


            }

            for (int i = 0; i < 14; i += 1)//由于y轴短一些，所以在此作出判断，只作25个刻度
            {
                //作出Y轴的刻度
                Line y_scale = new Line();
                y_scale.StrokeEndLineCap = PenLineCap.Triangle;
                y_scale.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0));

                y_scale.X1 = yuandian.X;            //原点x=40，在y轴上的刻度线的起点与原点相同
                y_scale.StrokeThickness = 0.1;

                y_scale.X2 = canvas.Width - rightM;
                y_scale.Y1 = topM + i * ((yuandian.Y - topM) / 13.0);  //每10px作一个刻度 
                y_scale.Y2 = y_scale.Y1;    //起点和终点y坐标相同 
                canvas.Children.Add(y_scale);

                if (i % 2 == 1)
                {
                    TextBlock y_ScaleLabel = new TextBlock();
                    y_ScaleLabel.TextAlignment = TextAlignment.Center;
                    y_ScaleLabel.Width = 30;
                    y_ScaleLabel.FontSize = 12;
                    y_ScaleLabel.Text = (-10 + i * 10).ToString();
                    Canvas.SetLeft(y_ScaleLabel, yuandian.X - 30);              //-25px是字体大小的偏移
                    Canvas.SetTop(y_ScaleLabel, topM + i * ((yuandian.Y - topM) / 13.0) - 10);  //280px是原点的坐标，同样-6是为了让标签不要上坐标轴叠上

                    canvas.Children.Add(y_ScaleLabel);
                }

            }
            {
                TextBlock x_ScaleLabel = new TextBlock();
                x_ScaleLabel.Text = "Hz"; ;//只给大刻度添加标签，每50px添加一个标签

                Canvas.SetLeft(x_ScaleLabel, canvas.Width - rightM + 12);//40是原点的坐标，-12是为了让标签看的位置剧中一点
                Canvas.SetTop(x_ScaleLabel, yuandian.Y + 10);//让标签字往下移一点

                canvas.Children.Add(x_ScaleLabel);

                TextBlock y_ScaleLabel = new TextBlock();

                y_ScaleLabel.Text = "dBHL";
                Canvas.SetLeft(y_ScaleLabel, yuandian.X - 30);
                Canvas.SetTop(y_ScaleLabel, topM - 30);

                canvas.Children.Add(y_ScaleLabel);
            }

        }
        /// <summary>
        /// 添加刻度标签
        /// </summary>
        //private void DrawScaleLabel(Canvas canvas)
        //{
        //    for (int i = 1; i < 7; i++)//7 个标签，一共
        //    {
        //        TextBlock x_ScaleLabel = new TextBlock();
        //        TextBlock y_ScaleLabel = new TextBlock();

        //        x_ScaleLabel.Text = (i * 50).ToString();//只给大刻度添加标签，每50px添加一个标签

        //        Canvas.SetLeft(x_ScaleLabel, 40 + 5 * 10 * i - 12);//40是原点的坐标，-12是为了让标签看的位置剧中一点
        //        Canvas.SetTop(x_ScaleLabel, 280 + 2);//让标签字往下移一点

        //        y_ScaleLabel.Text = (i * 50).ToString();
        //        Canvas.SetLeft(y_ScaleLabel, 40 - 25);              //-25px是字体大小的偏移
        //        Canvas.SetTop(y_ScaleLabel, 280 - 5 * 10 * i - 6);  //280px是原点的坐标，同样-6是为了让标签不要上坐标轴叠上

        //        canvas.Children.Add(x_ScaleLabel);
        //        canvas.Children.Add(y_ScaleLabel);
        //    }
        //}

        private void DrawPoint(Canvas canvas, List<Shape> list)
        {
            //随机生成8个点


            //Random rPoint = new Random();
            //for (int i = 0; i < 7; i++)
            //{
            //    int x_point = i * 60;
            //    int y_point = rPoint.Next(250);
            //    dataPoints.Add(new Point(x_point, y_point));
            //}

            //for (int i = 0; i < dataPoints.Count; i++)
            //{
            //    Ellipse dataEllipse = new Ellipse();
            //    dataEllipse.Fill = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));
            //    dataEllipse.Width = 8;
            //    dataEllipse.Height = 8;

            //    list.Add(dataEllipse);

            //    Canvas.SetLeft(list[i], 40 + dataPoints[i].X - 4);//-4是为了补偿圆点的大小，到精确的位置
            //    Canvas.SetTop(list[i], 280 - dataPoints[i].Y - 4);
            //    //将数据点在画布中的位置保存下来
            //    coordinatePoints.Add(new Point(40 + dataPoints[i].X, 280 - dataPoints[i].Y));
            //    canvas.Children.Add(list[i]);
            //}
        }

        private void DrawCurve(Canvas canvas)
        {
            curvePolyline.Points = coordinatePoints;
            canvas.Children.Add(curvePolyline);
        }
        private void AddCurvePoint(Point dataPoint)
        {

            dataPoints.Add(dataPoint);
            Ellipse dataEllipse = new Ellipse();
            dataEllipse.Fill = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));
            dataEllipse.Width = 8;
            dataEllipse.Height = 8;

            Random rPoint = new Random();
            //    Canvas.SetTop(list[i], 280 - dataPoints[i].Y - 4);
            //    //将数据点在画布中的位置保存下来
            coordinatePoints.Add(new Point(50 * rPoint.Next(7), dataPoint.Y));
            this.chartCanvas.Children.Add(dataEllipse);         //  int i = dataPoints.Count;

            //Random rPoint = new Random();
            // int x_point = i * 60;
            // int y_point = rPoint.Next(250);
            //  dataPoints.Add(new Point(x_point, y_point));
            // for (int i = 0; i < dataPoints.Count; i++)
            //{
            //每一个点的X数据都要向左移动50px
            //    dataPoints[i] = new Point(dataPoints[i].X - 50, dataPoints[i].Y);
            //    coordinatePoints[i] = new Point(40 + dataPoints[i].X, 280 - dataPoints[i].Y);

            // Canvas.SetLeft(pointEllipses[i], 40 + dataPoints[i].X - 4);//-4是为了补偿圆点的大小，到精确的位置
            //  Canvas.SetTop(pointEllipses[i], 280 - dataPoints[i].Y - 4);
            // }
        }

        void export(bool print)
        {
            //var fixedDoc = new FixedDocument();
            //var pageContent = new PageContent();
            //var fixedPage = new FixedPage();

            //fixedPage.Children.Add(canvas);
            //((IAddChild)pageContent).AddChild(fixedPage);
            //fixedDoc.Pages.Add(pageContent);

            //using var xpsd = new XpsDocument(@"r:\3.xps", FileAccess.ReadWrite);
            //var xw = XpsDocument.CreateXpsDocumentWriter(xpsd);

            //xw.Write(fixedDoc);
            //xpsd.Close();
            var basePath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            var path =  System.IO.Path.Combine(basePath, "DT261听力测试_"+DateTime.Now.ToString("yyyyMMddHHmmssffff")+".png");

            var model = DataContext as PreviewPageViewModel;

            SavePIC(path, model.CurrentIndex==0?docContent: docContent2, PICType.PNG, print);

           
        }

    
        private FixedDocument FD;

        public void AddPage(Visual V)
        {
            FrameworkElement FE = V as FrameworkElement;
            FixedPage FP = new FixedPage();
            FP.Width = FE.ActualWidth; //可用FE.Width替换
            FP.Height = FE.ActualHeight; //可用FE.Height替换
            FP.Children.Add(FE);
            PageContent pageContent = new PageContent();
            pageContent.Child = FP;
            FD.Pages.Add(pageContent);
        }



        private void SavePIC(string TempPath, Visual V, PICType PT,bool print)
        {
            FrameworkElement FE = V as FrameworkElement;


            ScrollViewer sv = V as ScrollViewer;

            var item = sv.Content as Grid;
            var width = item.RenderSize.Width;
            var height = item.RenderSize.Height;

            var drawingV = new DrawingVisual();
            var drawingB = new VisualBrush(item);

            using (var context = drawingV.RenderOpen())
            {
                context.DrawRectangle(drawingB, null, new Rect(new Point(0, 0), new Size(width,
                                                       height)));
            }
            RenderTargetBitmap RTB = new RenderTargetBitmap(
                                           (int)width,
                                           (int)height,
                                           96.0,
                                           96.0,

                                           PixelFormats.Pbgra32);

            RTB.Render(drawingV);
            Clipboard.SetImage(RTB);

            if(print)
            {
                PrintDialog diag = new PrintDialog();
                if(diag.ShowDialog()==true)
                {
                    diag.PrintVisual(drawingV, TempPath);
                }
                return;
            }

            BitmapEncoder BE = new BmpBitmapEncoder();

            switch (PT)
            {
                case PICType.PNG:
                    BE = new PngBitmapEncoder();
                    break;
                case PICType.JPG:
                    BE = new JpegBitmapEncoder();
                    break;
                case PICType.BMP:
                    BE = new BmpBitmapEncoder();
                    break;
                case PICType.GIF:
                    BE = new GifBitmapEncoder();
                    break;
                case PICType.TIFF:
                    BE = new TiffBitmapEncoder();
                    break;
                default:
                    BE = new BmpBitmapEncoder();
                    break;
            }
            GifBitmapEncoder a = new GifBitmapEncoder();
            BE?.Frames.Add(BitmapFrame.Create(RTB));
            using (FileStream fs = File.Open(TempPath, FileMode.OpenOrCreate))
            {
                BE.Save(fs);
            }
            Notice.Show("图片已保存到" + TempPath, "提示", 3, MessageBoxIcon.Success);
        }


        private RenderTargetBitmap GetPIC( Visual V)
        {
            FrameworkElement FE = V as FrameworkElement;

            var item = V as Canvas;
            var width = item.RenderSize.Width;
            var height = item.RenderSize.Height;

            var drawingV = new DrawingVisual();
            var drawingB = new VisualBrush(item);

            using (var context = drawingV.RenderOpen())
            {
                context.DrawRectangle(drawingB, null, new Rect(new Point(0, 0), new Size(width,
                                                       height)));
            }
            RenderTargetBitmap RTB = new RenderTargetBitmap(
                                           (int)width*2,
                                           (int)height*2,
                                           96.0*2,
                                           96.0*2,

                                           PixelFormats.Pbgra32);

            RTB.Render(drawingV);

            return RTB;
          
        }


        enum PICType
        {
            PNG = 0,
            JPG = 1,
            BMP = 2,
            GIF = 3,
            TIFF = 4

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            export(false);
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            export(true);
        }
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            DMSkin.Core.Broadcast.PushBroadcast("NavigationBack", "PreviewPage");
        }
        
    }
    }
